package com.hhsj.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.*;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
import com.hhsj.ui.R;

import java.util.Arrays;


@Deprecated
public class RCRelativeLayout extends RelativeLayout {
    public float[] radii = {0, 0, 0, 0, 0, 0, 0, 0};  // top-left, top-right, bottom-right, bottom-left
    public Path mClipPath;                            // 剪裁区域路径
    public Paint mPaint;                              // 画笔
    public int mEdgeFix = 10;                        // 边缘修复
    public RectF mLayer;                             // 画布图层大小

    public RCRelativeLayout(Context context) {
        this(context, null);
    }

    public RCRelativeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RCRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        obtainStyledAttrs(context, attrs, defStyleAttr);
        setWillNotDraw(false);
        mLayer = new RectF();
        mClipPath = new Path();
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setAntiAlias(true);
    }


    private void obtainStyledAttrs(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RCRelativeLayout, defStyleAttr, 0);
        int currRound = a.hasValue(R.styleable.RCRelativeLayout_radius) ? a.getDimensionPixelSize(R.styleable.RCRelativeLayout_radius, 0) : 0;
        Arrays.fill(radii, currRound);
        a.recycle();
    }

    @Override//当调用invalidate的时候会首先调用该处理
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mLayer.set(0, 0, w, h);
        refreshRegion();
    }

    @Override//在设置背景的情况下会调用draw方法 或者设置willnotdraw为false会调用因此要添加切割背景的处理
    public void draw(Canvas canvas) {
        refreshRegion();
        canvas.save();
        canvas.clipPath(mClipPath);
        super.draw(canvas);
        canvas.restore();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override//系统方法替代viewgroup的draw
    protected void dispatchDraw(Canvas canvas) {
        canvas.saveLayer(mLayer, null, Canvas.ALL_SAVE_FLAG);
        super.dispatchDraw(canvas);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawPath(mClipPath, mPaint);
        canvas.restore();
    }

    private void refreshRegion() {
        int w = (int) mLayer.width();
        int h = (int) mLayer.height();
        mClipPath.reset();
        mClipPath.addRoundRect(new RectF(getPaddingLeft(), getPaddingTop(), w - getPaddingRight(), h - getPaddingBottom()), radii, Path.Direction.CW);
        mClipPath.moveTo(-mEdgeFix, -mEdgeFix);  // 通过空操作让Path区域占满画布
        mClipPath.moveTo(w + mEdgeFix, h + mEdgeFix);
    }

    public void setRadius(int radius) {
        Arrays.fill(radii, radius);
        invalidate();
    }
}